package com.game.base.app.base;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;

import com.qmuiteam.qmui.util.QMUIStatusBarHelper;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

/**
 * Created by Su on 2019/5/8.
 */
public abstract class BaseActivity<T extends ViewDataBinding> extends AppCompatActivity {
    public BaseActivity mActivity;

    protected T binding;
    public Bundle mBundle;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        detectFileUriExposure();
        initBaseView();
        super.onCreate(savedInstanceState);
        initBaseData();
        initView();
        initData();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        mBundle = intent.getExtras();
    }


    /**
     * 获取布局文件
     */
    protected abstract int getLayoutId();

    /**
     * 初始化布局
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 初始化基础数据
     */
    private void initBaseData() {
        binding = DataBindingUtil.setContentView(this, getLayoutId());
    }

    /**
     * 用来屏蔽7.0中Uri的安全检测： Uri.fromFile爆出的FileUriExposureException
     * <p>
     * onCreate() 中的setContentView下调用
     */
    protected void detectFileUriExposure() {
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        if (Build.VERSION.SDK_INT >= 24) {
            builder.detectFileUriExposure();
        }
    }

    /**
     * 初始化基础布局
     */
    private void initBaseView() {
        mActivity = this;
        mBundle = getIntent().getExtras();
        QMUIStatusBarHelper.setStatusBarDarkMode(this);
    }
}
